import java.util.Arrays;

public class Stack1 {

   public static class Node{
       int val;
       Node next;
       public Node(int val) {
            this.val=val;
        }
    }
    public static Node head;
    //入栈
    public void push(int value) {
        Node node=new Node(value);
        node.next=head;
        head=node;
    }
    //出栈,并返回栈顶原来的元素
    public int pop () {
        if(head==null) {
            System.out.println("该栈没有元素");
            System.exit(0);
        }
        int n=head.val;
        head=head.next;
        return n;
    }
    // 查看栈顶元素
    public int peek() {
        if(head==null) {
            System.out.println("该栈没有元素");
            System.exit(0);
        }
        return head.val;
    }

    // 获取当前栈的长度
    public int getSize() {
      Node cur=head;
      int size=0;
        while (cur!=null) {
            cur=cur.next;
            size++;
        }
    return size;
    }

    @Override
    public String toString() {
       StringBuilder stringBuilder=new StringBuilder();
       stringBuilder.append('[');
       Node cur=head;
       while (cur!=null) {
           stringBuilder.append(cur.val);
           if(cur.next!=null) {
               stringBuilder.append(',');
           }
           cur=cur.next;
       }
       stringBuilder.append(']');
       return stringBuilder.toString();

    }
    public boolean StackEmpty() {
        return head==null;
    }
    public static void main(String[] args) {
        Stack1 stack1=new Stack1();
        stack1.push(1);
        stack1.push(2);
        stack1.push(3);
        stack1.push(4);
        stack1.push(5);
        System.out.println(stack1);
        System.out.println(stack1.getSize());
        System.out.println(stack1.peek());
    }
}
